package cn.mujiankeji.mbrowser.界面.标签页.网页

import android.view.MotionEvent
import android.webkit.WebView
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.remember
import kotlinx.coroutines.flow.MutableStateFlow

// 保存WebView滚动状态的类
class WebViewScrollState {
    // 是否允许Pager处理滑动事件
    val isPagerScrollEnabled = MutableStateFlow(true)
    // WebView是否已滚动到左/右边缘
    val isAtLeftEdge = MutableStateFlow(true)
    val isAtRightEdge = MutableStateFlow(true)
    // 保存上一次触摸事件的x坐标
    var lastTouchX: Float = 0f
    // 当前滑动方向 (-1: 左滑, 0: 未滑动, 1: 右滑)
    var currentSwipeDirection = 0
}

// CompositionLocal用于在Compose层级间共享WebViewScrollState
val LocalWebViewScrollState = compositionLocalOf<WebViewScrollState?> { null }

// 为WebView设置触摸事件监听器，确定滚动方向和边缘状态
fun WebView.setCoordinatedScrollListener(scrollState: WebViewScrollState) {
    this.setOnTouchListener { _, event ->
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                // 记录初始触摸位置
                scrollState.lastTouchX = event.x
                // 检测WebView是否在左右边缘
                scrollState.isAtLeftEdge.value = !canScrollHorizontally(-1)
                scrollState.isAtRightEdge.value = !canScrollHorizontally(1)
                false
            }
            MotionEvent.ACTION_MOVE -> {
                val deltaX = event.x - scrollState.lastTouchX
                // 确定滑动方向
                scrollState.currentSwipeDirection = when {
                    deltaX > 0 -> 1  // 右滑
                    deltaX < 0 -> -1 // 左滑
                    else -> 0        // 无水平滑动
                }
                
                // 更新滚动状态
                scrollState.isPagerScrollEnabled.value = when {
                    // 右滑且WebView已在左边缘，允许Pager处理
                    scrollState.currentSwipeDirection == 1 && scrollState.isAtLeftEdge.value -> true
                    // 左滑且WebView已在右边缘，允许Pager处理
                    scrollState.currentSwipeDirection == -1 && scrollState.isAtRightEdge.value -> true
                    // 其他情况WebView处理滑动
                    else -> false
                }
                
                scrollState.lastTouchX = event.x
                false
            }
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                // 重置状态
                scrollState.currentSwipeDirection = 0
                scrollState.isPagerScrollEnabled.value = true
                false
            }
            else -> false
        }
    }
    
    // 设置滚动变化监听器来更新边缘状态
    this.setOnScrollChangeListener { _, _, _, _, _ ->
        scrollState.isAtLeftEdge.value = !canScrollHorizontally(-1)
        scrollState.isAtRightEdge.value = !canScrollHorizontally(1)
    }
}

// Compose包装器，提供WebViewScrollState
@Composable
fun WebViewPagerCoordinator(content: @Composable () -> Unit) {
    val scrollState = remember { WebViewScrollState() }
    CompositionLocalProvider(LocalWebViewScrollState provides scrollState) {
        content()
    }
} 